function executeDeleteDs() {
    var dsId = $('#deleteDsDialog').find(":input[name='dsId']").val();
    $.ajax({
        type: "POST",
        url: "/dbConfig/delete",
        data: "id=" + dsId,
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return
            }
            alert("删除成功");
            window.location.reload();
        }

    });
}


function executeSaveDs() {
    var env = $($('#dsUpdateForm').find(":input")[0]).val();
    var url = $($('#dsUpdateForm').find(":input")[1]).val();
    var userName = $($('#dsUpdateForm').find(":input")[2]).val();
    var password = $($('#dsUpdateForm').find(":input")[3]).val();
    var id = $($('#dsUpdateForm').find(":input")[4]).val();

    var entity = new Object();
    entity.env = env;
    entity.url = url;
    entity.userName = userName;
    entity.password = password;
    entity.id = id;

    $.ajax({
        type: "POST",
        url: "/dbConfig/save",
        data: JSON.stringify(entity),
        contentType: "application/json",
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
                return
            }
            alert("保存成功");
            window.location.reload();
        }

    });

}

// 修改数据源时，弹出修改窗
function onClickDsUpdateBtn(id){
    $.ajax({
        type: "GET",
        url: "/dbConfig/preUpdate",
        data: "id=" + id,
        success: function (rp) {
            if (rp.fail) {
                alert(rp.message);
            }
            var data = rp.data;
            $($('#dsUpdateForm').find(":input")[0]).val(data.env);
            $($('#dsUpdateForm').find(":input")[1]).val(data.url);
            $($('#dsUpdateForm').find(":input")[2]).val(data.userName);
            $($('#dsUpdateForm').find(":input")[3]).val(data.password);
            $($('#dsUpdateForm').find(":input")[4]).val(data.id);
        }
    });
}

$(function ($) {

    function loadDsList() {
        $.ajax({
            type: "GET",
            url: "/dbConfig/list",
            success: function(rp){
                var data = rp.data;
               // alert( "Data list: " + data );
                
                if (rp.fail) {
                    alert(rp.message);
                    return;
                }

                // 先清空，再添加
                $('#dsTb').empty();
                for (var i = 0; i < data.length; i++) {

                    var content = '<div class="row">\n' +
                        '<div class="col-xs-1 ">\n' +
                        data[i].id +
                        '</div>\n' +
                        '<div class="col-xs-1">\n' +
                        data[i].env +
                        '                            </div>\n' +
                        '<div class="col-xs-6">\n' +
                        data[i].url +
                        '</div>\n' +
                        '<div class="col-xs-1">\n' +
                                    data[i].userName +
                        '</div>\n' +
                        '    <div class="col-xs-1">\n' +
                                data[i].password +
                        '    </div>\n' +
                        '    <div class="col-xs-1">\n' +
                        '        <button dsId="' + data[i].id + '" class="btn btn-success btn-xs" data-toggle="modal" data-target="#changeDsDialog">修改</button>\n' +
                        '        <button dsId="' + data[i].id + '" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deleteDsDialog">删除</button>\n' +
                        '    </div>\n' +
                        '</div>';
                   // alert(content)
                    $('#dsTb').append(content);
                }

            }
        });
    }

    // 添加数据源
    function addDs() {
        var env = $('#dsAddForm').find(":input")[0].value;
        var url = $('#dsAddForm').find(":input")[1].value;
        var userName = $('#dsAddForm').find(":input")[2].value;
        var password = $('#dsAddForm').find(":input")[3].value;
        //alert(env);
        var obj = new Object();
        obj.env = env;
        obj.url = url;
        obj.userName = userName;
        obj.password = password;
        $.ajax({
            type: "POST",
            url: "/dbConfig/save",
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: function (rp) {
                if (rp.fail) {
                    alert(rp.message);
                    return;
                }
                alert("保存成功");
                window.location.reload();
            }

        });
    }

    // 给数据源列表菜单绑定事件
    $('#dsMenu').bind("click", function(){
        loadDsList();
    });


    // 添加新的数据源
    $('#dsAddBtn').bind("click", function () {
        addDs();
    });


    /**
     * 给修改模态框弹出时绑定事件
     * @link https://blog.csdn.net/luckyjiexu/article/details/68062634
     */
    $('#changeDsDialog').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // 触发事件的按钮
        var dsId = button.attr("dsId");
        onClickDsUpdateBtn(dsId);
    });

    /**
     * 给删除模态框弹出时绑定事件
     */
    $('#deleteDsDialog').on('show.bs.modal', function (event) {
        var dsId = $(event.relatedTarget).attr("dsId"); // 触发事件的按钮
        $(this).find(":input[name='dsId']").val(dsId);
    });


});